package com.tuxianchao.demo.springbootactuator.controller;

import com.tuxianchao.demo.springbootactuator.entity.User;
import com.tuxianchao.demo.springbootactuator.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * created by
 *
 * @author tuxianchao
 * @date 2018/6/3 下午3:47
 */
@Controller
public class UserController {
    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/initUser")
    @ResponseBody
    public Object initUser() {
        // 创建100条记录
        for (int i = 0; i < 100; i++) {
            userRepository.save(new User(
                    UUID.randomUUID().toString().replace("-", "").substring(1, 5)
                    , 10));
        }

        return "success";

    }

    @RequestMapping("/users")
    @ResponseBody
    public Object getAllUser() {
        return userRepository.findAll();
    }

    @RequestMapping("/user")
    @ResponseBody
    public Object getUser(@RequestParam(name = "name", required = true) String name) {
        return userRepository.findByName(name);
    }


}
